package com.cjack.frenchexcel.dao;

import com.cjack.frenchexcel.table.GoodsTable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by root on 10/5/19
 */
public interface GoodsDao extends JpaRepository<GoodsTable, Long>, JpaSpecificationExecutor<GoodsTable> {

    //通过海关码和月份查询  当月入库的同类 商品列表 ,并根据order从小到大排序
    List< GoodsTable> findAllByMonthAndCustomHouseOrderByNumberDesc( Integer month, String customHouse);

    GoodsTable getByConsumerRecordId( Long consumerRecordId);

    //通过月份查询  商品列表 ,并根据order从小到大排序
    List< GoodsTable> findAllByMonthOrderByNumberDesc( Integer month);

    @Transactional( timeout = 100)
    @Modifying
    @Query( value = "insert into fe_goods(id,consumer_record_id,create_time,custom_house,customs_clearance_form,month,name,number,orderg,status,update_time) VALUES(:#{#data.id},:#{#data.consumerRecordId},:#{#data.createTime},:#{#data.customHouse},:#{#data.customsClearanceForm},:#{#data.month},:#{#data.name},:#{#data.number},:#{#data.orderG},:#{#data.status},:#{#data.updateTime})", nativeQuery = true)
    void restoreData( @Param("data") GoodsTable table);

    /**
     * 清除所有数据
     */
    @Transactional( timeout = 100)
    @Modifying
    @Query( value = " delete from fe_goods where 1=1", nativeQuery = true)
    void clearAll();
}
